<?php

require_once   BaseUtil::getModulePath("base")  .  '/validate/Unique.php';

class RoleForm extends Zend_Form
{
    public function __construct ($permissions,$role = null,$rolePermissions=null)
    {
        //for i18n
        $translate = Zend_Registry::get('Zend_Translate');

        $this->setMethod('post');
        $this->setName('Role_Form');
        $nameField = new Zend_Form_Element_Text('name');
        $nameField->setLabel($translate->_('name'));
        $nameField->addFilter("StringTrim");
        $nameField->setRequired(true);

        //unique validate
        $validUnique = new MyValid_Unique();
        $validUnique->setTableName('roles');
        $validUnique->setFieldName('role_name');
        if(!empty($role['id'])){
            $validUnique->setId($role['id']);
        }
        $nameField->addValidator($validUnique);

        if(isset($role)){
            $this->setAction(Zend_View_Helper_Url::url(array('controller' => 'acl' , 'action' => 'editrolesubmit')));
            $nameField->setValue($role['role_name']);
        }else{
            $this->setAction(Zend_View_Helper_Url::url(array('controller' => 'acl' , 'action' => 'addrolesubmit')));
        }
        
        if(!empty($role['id'])){
			$id = new Zend_Form_Element_Hidden('role_id');
			$id->setValue($role['id']);
            $this->addElement($id);
		}
        //set permission element
        if(!empty($permissions)){
            $permissionChkbox = new Zend_Form_Element_MultiCheckbox("rolePermission");
            $permissionChkbox->setLabel($translate->_('permission'));
            $permissionChkbox->setRequired(true);
            $permissionChkbox->setMultiOptions($permissions);
            if(!empty($rolePermissions)){
                $permissionChkbox->setValue($rolePermissions);
            }
        }

        $elements = array($nameField,$permissionChkbox);
        $this->addElements($elements);
        $this->setElementDecorators(array(
            'ViewHelper',
            array(array('div' => 'HtmlTag'), array('tag' => 'div',"class" => "form_element")),
        ));
    }
}
